package com.zhoutao123.hephaestus.object.transport;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.zhoutao123.hephaestus.repository.entity.Blog;
import lombok.Data;
import lombok.experimental.Accessors;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Data
@Accessors(chain = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class BlogTO {

    private String id;

    private String title;

    private String htmlContent;

    private String mkContent;

    private String description;

    private List<String> typeId;

    private String keyWords;

    private Boolean allowComment = Boolean.TRUE;

    private Boolean allowTop = Boolean.FALSE;

    /**
     * 生成创建文章实体
     *
     * @return
     */
    public Blog toEntity() {
        Boolean isUpdate = StringUtils.hasText(id);
        Blog blog = new Blog();
        blog.setId(isUpdate ? id : UUID.randomUUID().toString());
        blog.setContent(htmlContent);
        blog.setContentMd(mkContent);
        blog.setCreateTime(isUpdate ? null : new Date());
        blog.setUpdateTime(new Date());
        blog.setDescription(description);
        blog.setKeywords(keyWords);
        blog.setRecommended(allowComment);
        blog.setTitle(title);
        blog.setTop(allowTop);
        return blog;
    }
}
